/**
 * 1.引入文件const Foo = { template: '<div>foo</div>' }
 * 2.定义routes
 * const routes = [
        { path: '/foo', component: Foo },
        { path: '/bar', component: Bar }
    ]
 * 3.Create the router instance and pass the `routes` option
    const router = new VueRouter({
        routes // short for `routes: routes`
    })
   4.导出 router
   5.在main中引入挂载在app上
 */

import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '@/views/login.vue'
import Layout from '@/layout/index.vue'


Vue.use(VueRouter)

const originalPush = VueRouter.prototype.push

VueRouter.prototype.push = function push(location) {

    return originalPush.call(this, location).catch(err => err)

}
export const routes = [{
        path: '/redirect',
        component: Layout,
        hidden: true,
        children: [{
            path: '/redirect/:path(.*)',
            component: (resolve) => require(['@/views/redirect'], resolve)
        }]
    },
    {
        path: '/login',
        component: Login,
        name: 'Login',
        hidden: true
    },
    {
        path: '/404',
        component: (resolve) => require(['@/views/error/404'], resolve),
        hidden: true,
    },
    {
        path: '/',
        component: Layout,
        redirect: 'index',
        children: [{
            path: '/index',
            component: (resolve) => require(['@/components/HelloWorld'], resolve),
            name: '首页',
            meta: {
                title: '首页',
                icon: 'dashboard',
                noCache: true,
                affix: true
            }
        }]
    },
    {
        path: '/nested',
        component: Layout,
        redirect: 'noRedirect',
        name: 'Nested',
        meta: {
            title: '价格确定',
            icon: 'nested'
        },
        children: [{
                path: 'menu1',
                component: () => import('@/views/nested/menu1/index'), // Parent router-view
                name: 'Menu1',
                redirect: 'noRedirect',
                meta: {
                    title: '基本信息'
                },
                children: [{
                        path: 'menu1-1',
                        component: () => import('@/views/nested/menu1/menu1-1'),
                        name: 'HouseList',
                        meta: {
                            title: '房源列表'
                        }
                    },
                    // {
                    //     path: 'menu1-2',
                    //     component: () => import('@/views/nested/menu1/menu1-2'),
                    //     name: 'Menu1-2',
                    //     meta: {
                    //         title: 'Menu1-2'
                    //     },
                    //     children: [{
                    //             path: 'menu1-2-1',
                    //             component: () => import('@/views/nested/menu1/menu1-2/menu1-2-1'),
                    //             name: 'Menu1-2-1',
                    //             meta: {
                    //                 title: 'Menu1-2-1'
                    //             }
                    //         },
                    //         {
                    //             path: 'menu1-2-2',
                    //             component: () => import('@/views/nested/menu1/menu1-2/menu1-2-2'),
                    //             name: 'Menu1-2-2',
                    //             meta: {
                    //                 title: 'Menu1-2-2'
                    //             }
                    //         }
                    //     ]
                    // },
                    {
                        path: 'menu1-3',
                        component: () => import('@/views/nested/menu1/menu1-3'),
                        name: 'addHouse',
                        meta: {
                            title: '添加房源'
                        }
                    }
                ]
            },
            // {
            //     path: 'menu2',
            //     component: () => import('@/views/nested/menu2/index'),
            //     name: 'Menu2',
            //     meta: {
            //         title: 'menu2'
            //     }
            // }
        ]
    },
    {
        path: '/form',
        component: Layout,
        children: [{
            path: 'index',
            name: 'Form',
            component: () => import('@/views/form/index'),
            meta: {
                title: '修改密码',
                icon: 'form'
            }
        }]
    },
]
// export default new VueRouter({
//     mode: 'history',
//     scrollBehavior: () => ({
//         y: 0
//     }),
//     routes
// })
const createRouter = () => new VueRouter({
    // mode: 'history', // require service support
    scrollBehavior: () => ({ y: 0 }),
    routes: routes
  })
  
  const router = createRouter()
  
  // Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
  export function resetRouter() {
    const newRouter = createRouter()
    router.matcher = newRouter.matcher // reset router
  }
  
  export default router
  